//
//  AppDelegate.m
//  didacar
//
//  Created by 度周末网络-王腾 on 15/11/20.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "AppDelegate.h"
#import "GpsManager.h"
#import <MAMapKit/MAMapKit.h>
#import "UmengTrackObject.h"
#import "APService.h"
#import "UMSocialSnsService.h"
#import "CommonAddressViewModel.h"
#import "WXApiManager.h"
#import "WXApi.h"
#import "AlipayManager.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.cityViewModel = [[CityViewModel alloc ] init];
    [self.cityViewModel loadData:^(NSInteger resultType, id data, NSString *errorMsg) {
        if (resultType == 1) {
            // 定位
            [[GpsManager shareManager] startGPS];
        }
    }];
    
    [CommonAddressViewModel getMyAddress:^(NSInteger resultType, id data, NSString *errorMsg) {
        
    }];
    
    // 友盟分享配置
    [[UmengTrackObject sharedManager] umengShareConfig];
    
    [self GDMapConfig];
    
    [self configJPush:launchOptions];
    
    [WXApi registerApp:@"wx7fe601695e9ef672" withDescription:@"com.bus.passenger"];
    
    return YES;
}

/**
 *  配置JPush
 *
 *  @param launchOptions 启动时的参数
 */
-(void)configJPush:(NSDictionary *)launchOptions
{
    [APService setupWithOption:launchOptions];
    [APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                   UIRemoteNotificationTypeSound |
                                                   UIRemoteNotificationTypeAlert)
                                       categories:nil];
    
    [APService resetBadge];
    
}

// 高德地图
-(void)GDMapConfig{
    // 高德地图初始化
    [MAMapServices sharedServices].apiKey = @"cf235d285d058d4336262bcdd99db3f8";
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    
    if ([[url absoluteString] containsString:@"oauth"] || [[url absoluteString] containsString:@"qzapp"] || [[url absoluteString] containsString:@"1922122420"]) {
        return [UMSocialSnsService handleOpenURL:url];
    }
    // 微信支付
    if ([[url absoluteString] containsString:@"wx7fe601695e9ef672"]) {
        return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
    }else{
         [[AlipayManager sharedManager] alipay:url application:application];
    }
    return  YES;
  
   
}

- (void)applicationWillResignActive:(UIApplication *)application {
  
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
   
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [APService registerDeviceToken:deviceToken];
    /*
     NSString *pushToken = [[[[deviceToken description]
     stringByReplacingOccurrencesOfString:@"<" withString:@""]
     stringByReplacingOccurrencesOfString:@">" withString:@""]
     stringByReplacingOccurrencesOfString:@" " withString:@""];
     */
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [application setApplicationIconBadgeNumber:0];
    [UMSocialSnsService  applicationDidBecomeActive];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
   
}

- (void)applicationWillTerminate:(UIApplication *)application {
   
}

@end
